package com.tsu.mall.order.Controller;

import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.entity.Member;
import com.tsu.mall.common.service.IMemberService;
import com.tsu.mall.common.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/personalInfo")
public class personalInfoController {
    @Autowired
    private IMemberService iMemberService;
    @GetMapping("/personalInfoPage")
    public String personalInfo(Model model) {
        //查询当前登录用户信息
        Member currentMember=iMemberService.doSelectById(RequestUtil.getCurrent().getId());
        model.addAttribute("currentMember",currentMember);
        return "personalInfo";
    }
    @PostMapping("/currentMemberInfo")
    @ResponseBody
    public Member currentMemberInfo(){
        return iMemberService.doSelectById(RequestUtil.getCurrent().getId());
    }
    @PostMapping("/saveMemberInfo")
    @ResponseBody
    public void saveMemberInfo(Member member){
        iMemberService.saveOrUpdate(member);
    }

    //判重操作
    @PostMapping("/accountNumber")
    @ResponseBody
    public String accountNumber(String accountNumber){
        return iMemberService.selectAccountNumber(accountNumber,RequestUtil.getCurrent().getId());
    }
    @PostMapping("/avatar")
    @ResponseBody
    public LayuiTableVO avatar(MultipartFile file){
        String fileName=System.currentTimeMillis()+".jpg";
        File f=new File("D:/test/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LayuiTableVO vo=new LayuiTableVO();

        String s="/upload/"+fileName;
        Map<String ,String> map=new HashMap<>();
        map.put("avatar",s);
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }
    @PostMapping("/updateMemberAvatar")
    @ResponseBody
    public void updateMemberAvatar(String avatar){
        iMemberService.updateMemberAvatar(avatar,RequestUtil.getCurrent().getId());
    }
    @PostMapping("/selectCurrentAvatar")
    @ResponseBody
    public String selectCurrentAvatar(){
        return iMemberService.selectCurrentAvatar(RequestUtil.getCurrent().getId());
    }
}
